package com.xxx.store.utils;


//import org.apache.mahout.cf.taste.common.TasteException;
//import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
//import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
//import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
//import org.apache.mahout.cf.taste.model.DataModel;
//import org.apache.mahout.cf.taste.recommender.RecommendedItem;
//import org.apache.mahout.cf.taste.similarity.ItemSimilarity;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.*;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.apache.mahout.math.hadoop.similarity.cooccurrence.measures.CosineSimilarity;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * TODO
 *
 * @Date 2022/3/8
 * @Created by FGH
 */
public class BaseItemRecommender {
    public static void main(String[] args) throws IOException, TasteException {
        //准备数据 这里是电影评分数据
        File file = new File("/Users/laiyu/Desktop/ml-latest-small/ratings.csv");
        //将数据加载到内存中，GroupLensDataModel是针对开放电影评论数据的
//        DataModel dataModel = new FileDataModel(file);
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setServerName("localhost");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setDatabaseName("a5116yishu");

        DataModel dataModel = new MySQLJDBCDataModel(dataSource, "user_look","user_id","product_id", "count", null);

        //计算相似度，相似度算法有很多种，欧几里得、皮尔逊等等。
        UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
//        UserSimilarity similarity1 = new UncenteredCosineSimilarity(dataModel);
//        EuclideanDistanceSimilarity euclideanDistanceSimilarity = new EuclideanDistanceSimilarity(dataModel);

        ItemSimilarity similarity2 = new PearsonCorrelationSimilarity(dataModel);//计算内容相似度
        Recommender recommender2 = new GenericItemBasedRecommender(dataModel, similarity2);//构造推荐引擎

        //计算最近邻域，邻居有两种算法，基于固定数量的邻居和基于相似度的邻居，这里使用基于固定数量的邻居
        UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(100,similarity, dataModel);
//        UserNeighborhood neighborhood = new ThresholdUserNeighborhood(100.0, euclideanDistanceSimilarity, dataModel);
        //构建推荐器，协同过滤推荐有两种，分别是基于用户的和基于物品的，这里使用基于用户的协同过滤推荐
        Recommender recommender = new GenericUserBasedRecommender(dataModel, userNeighborhood, similarity);
//        new GenericItemBasedRecommender(dataModel, GenericItemSimilarity).
        //给用户ID等于5的用户推荐10部电影
        List<RecommendedItem> recommendedItemList = recommender.recommend(1, 100);
        //打印推荐的结果
        System.out.println("使用基于用户的协同过滤算法");
        for (RecommendedItem recommendedItem : recommendedItemList) {
            System.out.println(recommendedItem);
        }
//        System.out.println("========================");
//        List<RecommendedItem> recommend = recommender2.recommend(1, 10);
//        recommend.forEach(System.out::println);
    }
}
